package com.example.combwyaq.visualinterface.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.example.combwyaq.R;
import com.example.combwyaq.visualinterface.mainfragment.MainFragment_Live;
import com.example.combwyaq.visualinterface.mainfragment.MainFragment_Mine;
import com.example.combwyaq.visualinterface.mainfragment.MainFragment_Video;
import com.example.combwyaq.visualinterface.mainfragment.MainFragment_pop;
import com.example.combwyaq.visualinterface.news.NewsFragment;
import com.example.combwyaq.visualinterface.people.PeoPleFragment;
import com.example.combwyaq.visualinterface.video.VideoFragment;
import com.example.combwyaq.widget.BottomButton;
import com.example.combwyaq.widget.DotWidget;

public class FragmentActivity extends AppCompatActivity implements BottomButton.SelectListener {

    private BottomButton newsBottom;
    private BottomButton peopleBottom;
    private BottomButton videoBottom;
    private BottomButton liveBottom;
    private BottomButton mineBottom;
    private DotWidget redDotView;
    private LinearLayout llFragment;
    private String TAG ="changer";

    MainFragment_Mine mainFragment_mine;
    VideoFragment videoFragment;
    MainFragment_Live mainFragment_live;
//    MainFragment_pop mainFragment_pop;
    NewsFragment newsFragment;
    PeoPleFragment peoPleFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_button);
        initView();
        newsBottom.setContent("新闻").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.tu1_f)
                .setSelImg(R.drawable.tu1_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        peopleBottom.setContent("人民号").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.tu2_f)
                .setSelImg(R.drawable.tu2_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        videoBottom.setContent("视频").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.tu3_f)
                .setSelImg(R.drawable.tu3_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        liveBottom.setContent("直播").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.tu4_f)
                .setSelImg(R.drawable.tu4_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        mineBottom.setContent("我的").setNomalColor(Color.BLACK)
                .setSelColor(Color.RED)
                .setNomalImg(R.drawable.tu5_f)
                .setSelImg(R.drawable.tu5_t)
                .setListener(this::onSelect)
                .setShowPoint(false);
        change(mineBottom);

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        newsFragment=new NewsFragment();
        fragmentTransaction.add(R.id.ll_fragment,newsFragment);
        fragmentTransaction.commit();


    }



    public void change(View view) {
        if (redDotView.getIsShow()) redDotView.setIsShow(false);
        else redDotView.setIsShow(true);
    }

    private void initView() {
        newsBottom = (BottomButton) findViewById(R.id.news_bottom);
        peopleBottom = (BottomButton) findViewById(R.id.people_bottom);
        videoBottom = (BottomButton) findViewById(R.id.video_bottom);
        liveBottom = (BottomButton) findViewById(R.id.live_bottom);
        mineBottom = (BottomButton) findViewById(R.id.mine_bottom);
        redDotView = (DotWidget) findViewById(R.id.redDotView);
        llFragment = (LinearLayout) findViewById(R.id.ll_fragment);


    }

    @Override
    public void onSelect(int id) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (newsFragment!=null){
            fragmentTransaction.hide(newsFragment);
        }
        if (peoPleFragment!=null){
            fragmentTransaction.hide(peoPleFragment);
        }
        if (mainFragment_live!=null){
            fragmentTransaction.hide(mainFragment_live);
        }
        if (mainFragment_mine!=null){
            fragmentTransaction.hide(mainFragment_mine);
        }
        if (videoFragment!=null){
            fragmentTransaction.hide(videoFragment);
        }

        if (id == R.id.news_bottom) {
            Log.i(TAG, "onSelect: ");
            peopleBottom.clearSelect();
            videoBottom.clearSelect();
            liveBottom.clearSelect();
            mineBottom.clearSelect();
            fragmentTransaction.show(newsFragment);
        } else if (id == R.id.people_bottom) {
            newsBottom.clearSelect();
            videoBottom.clearSelect();
            liveBottom.clearSelect();
            mineBottom.clearSelect();
            if (peoPleFragment==null){
                peoPleFragment=new PeoPleFragment();
                fragmentTransaction.add(R.id.ll_fragment, peoPleFragment);
            }else{
                fragmentTransaction.show(peoPleFragment);
            }
        } else if (id == R.id.video_bottom) {
            Log.i(TAG, "onSelect: ");
            newsBottom.clearSelect();
            peopleBottom.clearSelect();
            liveBottom.clearSelect();
            mineBottom.clearSelect();
            if (videoFragment==null){
                videoFragment=new VideoFragment();
                fragmentTransaction.add(R.id.ll_fragment, videoFragment);
            }else{
                fragmentTransaction.show(videoFragment);
            }
        } else if (id == R.id.live_bottom) {
            Log.i(TAG, "onSelect: ");
            newsBottom.clearSelect();
            peopleBottom.clearSelect();
            videoBottom.clearSelect();
            mineBottom.clearSelect();
            if (mainFragment_live == null) {
                mainFragment_live = new MainFragment_Live();
                fragmentTransaction.add(R.id.ll_fragment, mainFragment_live);
            } else {
                fragmentTransaction.show(mainFragment_live);
            }

        } else if (id == R.id.mine_bottom) {
            Log.i(TAG, "onSelect: ");
            newsBottom.clearSelect();
            peopleBottom.clearSelect();
            videoBottom.clearSelect();
            liveBottom.clearSelect();
            if (mainFragment_mine == null) {
                mainFragment_mine = new MainFragment_Mine();
                fragmentTransaction.add(R.id.ll_fragment, mainFragment_mine);
            } else {
                fragmentTransaction.show(mainFragment_mine);
            }
        }
        fragmentTransaction.commit();
    }
}
